package com.phone.servlet.invite;

import com.phone.DBHelper;
import com.phone.entity.Invite;
import com.phone.entity.Result;
import com.phone.exception.BaseException;
import com.phone.handle.InviteHandle;
import com.phone.servlet.BaseServlet;
import org.hibernate.HibernateException;
import org.hibernate.Session;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by tisen on 2017/7/19.
 */
public class Get extends BaseServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        setParamterRequest(request);
        Session session = DBHelper.getSession();
        session.beginTransaction();
        Result<Invite> result = new Result<>();
        InviteHandle handle = new InviteHandle(session);
        try {
            String id = getParamterString("id",false);
            result.setObject(handle.get(id));
            result.setResult(SUCCESS);
            result.setReason("get invite successful");
            session.getTransaction().commit();
        } catch (HibernateException exception) {
            exception.printStackTrace();
            result.setReason("sql error");
            session.getTransaction().rollback();
        } catch (BaseException exception) {
            exception.printStackTrace();
            result.setReason(exception.getMessage());
            session.getTransaction().rollback();
        } catch (Exception e) {
            result.setReason(e.getMessage());
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen())
                session.close();
            out(response, result);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
